<?php
if(isset($_POST['submit']))
{
	$database =  $_POST['database_name'] ;
	$username =  $_POST['user_name'] ;
	if(isset($_POST['password'])){
            $password= $_POST['password'];
        }
        else{
            $password = "";
        }
	$database_host =  $_POST['database_host'];
	$newUrl =  "http://".$_POST['new_url'];
	
	$db = new mysqli($database_host, $username, $password, $database);
	//get old url
	$query = "SELECT option_value FROM wp_options WHERE option_name ='siteurl'";
	$result = $db->query($query);
	$row = $result->fetch_object();
	$oldUrl = $row->option_value;
	
	//update siteurl
	$query = "UPDATE ".$database.".wp_options SET option_value ='".$newUrl."' WHERE option_name='siteurl' or option_name='home'";
		echo $query.'<br/>';
		echo $db->field_count." rows where changed<br/>";
	$result = $db->query($query);
	$query = "UPDATE ".$database.".wp_postmeta SET meta_value = REPLACE(meta_value, '".$oldUrl."', '".$newUrl."')";
		echo $query.'<br/>';
		echo $db->field_count." rows where changed<br/>";
	$result = $db->query($query);
	$query = "UPDATE ".$database.".wp_posts SET guid = REPLACE(guid, '".$oldUrl."', '".$newUrl."')";
		echo $query.'<br/>';
		echo $db->field_count." rows where changed<br/>";
	$result = $db->query($query);
	$query = "UPDATE ".$database.".wp_posts SET post_content = REPLACE(post_content, '".$oldUrl."', '".$newUrl."')";
		echo $query.'<br/>';
		echo $db->field_count." rows where changed<br/>";
	$result = $db->query($query);
	
	
	
	
	
}	
?>

<html>
<title>
Wordpress Database Converter
</title>
<head>

</head>
<body>
<?php
if(!isset($_POST['submit']))
{

?>
<form name="getDatabase" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div>
	<label>Database Name</label><input type="text" name="database_name"/>
</div>
<div>
	<label>User Name</label><input type="text" name="user_name"/>
</div>
<div>
	<label>Password</label><input type="text" name="password"/>
</div>
<div>
	<label>Database Host</label><input type="text" name="database_host"/>
</div>
<div>
	<label>New Site URL</label>http://<input type="text" name="new_url"/>
</div>
<div>
<input type="submit" value="submit" name="submit"/>
</div>
</form>
<?php
}
?>

</body>
</html>